<?php
// +----------------------------------------------------------------------
// | Huncent Tinysupe [ SIMPLE IS THE BEST ! ]
// +----------------------------------------------------------------------
// | Copyright (c) 2011 ZMRNet Studio, All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: tinysu <tinysu@qq.com>
// +----------------------------------------------------------------------

// user functions
function runtime(){
    return round((microtime(true) - RUNTIME) * 1000, 3);
}

function fmtdate($time) {
	return date('Y-m-d H:i:s', $time + 28800);
}

function fmtsize($size, $prec=2) {
	$unit = 'b';
	$size = floatval($size);
	if($size < 0) return '';
	if($size > 1024){ $size /= 1024; $unit = 'k'; }
	if($size > 1024){ $size /= 1024; $unit = 'm'; }
	if($size > 1024){ $size /= 1024; $unit = 'g'; }
	if($size > 1024){ $size /= 1024; $unit = 't'; }
	return round($size, $prec) . $unit;
}

function cutstr($str, $len){
	return (strlen($str) > $len) ? mb_substr($str, 0, $len, 'utf-8') : $str;
}

function weburl($url){
	return _WEBDIR . $url . _WEBEXT;
}

// core functions
function ht_get($key){
	return isset($_GET[$key]) ? trim($_GET[$key]) : '';
}

function ht_post($key){
	return isset($_POST[$key]) ? trim($_POST[$key]) : '';
}

function ht_session($key){
	return isset($_SESSION[$key]) ? trim($_SESSION[$key]) : '';
}

function ht_sitedir(){
	return str_replace('//', '/', str_replace('\\', '/', dirname($_SERVER['SCRIPT_NAME'])) .'/');
}

function ht_squotes($str){
	return str_replace('\'\'', '\'', $str);
}

function ht_userip(){
	if(@$_SERVER['HTTP_CLIENT_IP'] && $_SERVER['HTTP_CLIENT_IP']!='unknown'){
		$clientip = $_SERVER['HTTP_CLIENT_IP'];
	}elseif(@$_SERVER['HTTP_X_FORWARDED_FOR'] && $_SERVER['HTTP_X_FORWARDED_FOR']!='unknown'){ 
		$clientip = $_SERVER['HTTP_X_FORWARDED_FOR'];
	}else{ 
		$clientip = $_SERVER['REMOTE_ADDR'];
	}
	return $clientip;
}

function ht_error404(){
	header('HTTP/1.0 404 Not Found');
	exit('<h2>Error 404 - Not Found</h2>');
}
?>